package com.example.moblie203zlw_databases11_05.slice;

import com.example.moblie203zlw_databases11_05.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;
import ohos.data.DatabaseHelper;
import ohos.data.rdb.RdbPredicates;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;

public class my_updateSlice extends AbilitySlice {
    Text T_name,T_sex,T_phone;
    Button but_xuigai;
    String sex,name;
    int phone;
    DatabaseHelper databaseHelper;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_my_update);
        T_name= (Text) findComponentById(ResourceTable.Id_name);
        T_sex= (Text) findComponentById(ResourceTable.Id_sex);
        T_phone=(Text) findComponentById(ResourceTable.Id_phone);
        name = intent.getStringParam("name");
        sex = intent.getStringParam("sex");
        T_name.setText("姓名:"+name);
        T_sex.setText("性别:"+sex);
        phone = intent.getIntParam("phone",1);
        String tt= String.valueOf(phone);
        T_phone.setText("电话:"+tt);
        but_xuigai= (Button) findComponentById(ResourceTable.Id_but);
        databaseHelper=new DatabaseHelper(this);
        but_xuigai.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                id();
            }
        });
    }

    private void id() {
        CommonDialog commonDialog=new CommonDialog(this);
        Component component_dialog= LayoutScatter.getInstance(my_updateSlice.this)
                .parse(ResourceTable.Layout_dialog_add_contacts,null,true);
        TextField nameTf = (TextField)
                component_dialog.findComponentById(ResourceTable. Id_tf_name );
        TextField  phoneTf = (TextField)
                component_dialog.findComponentById(ResourceTable. Id_tf_phone );
        Button btnConfirm = (Button)
                component_dialog.findComponentById(ResourceTable. Id_btn_confirm );
        Button btnCacel = (Button)
                component_dialog.findComponentById(ResourceTable. Id_btn_cancel );
        RadioContainer  genderRc = (RadioContainer)
                component_dialog.findComponentById(ResourceTable. Id_rc_gender );
        RadioButton  manRb = (RadioButton)
                component_dialog.findComponentById(ResourceTable. Id_rb_man );
        RadioButton  ladyRb = (RadioButton)
                component_dialog.findComponentById(ResourceTable. Id_rb_lady );
        if (sex.equals("男")){
            manRb.setChecked(true);
        }else if (sex.equals("女")){
            ladyRb.setChecked(true);
        }
        nameTf.setText(name);
        String iphone= String.valueOf(phone);
        phoneTf.setText(iphone);
        btnConfirm.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                RdbPredicates rdbPredicates=new RdbPredicates("mobile").equalTo("name",name);//用表查
                String s[]={"name","sex","phone"};
                String name=nameTf.getText();
                String phone=phoneTf.getText();
                if (name!=null && phone!=null){
                    ValuesBucket valuesBucket=new ValuesBucket();
                    valuesBucket.putString("name",name);
                    valuesBucket.putString("sex",sex);
                    valuesBucket.putString("phone",phone);
                    int resultSet_up=MainAbilitySlice.getRdbStore().update(valuesBucket,rdbPredicates);
                    System.out.println(resultSet_up);
                    Intent intent=new Intent();
                    present(new MainAbilitySlice(),intent);
                }
                commonDialog.remove();
            }
        });
        btnCacel.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                commonDialog.remove();
            }
        });
        commonDialog.setContentCustomComponent(component_dialog);
        commonDialog.show();
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
